package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.OpenAreaDao;
import cn.edu.sdjzu.xg.bysj.domain.OpenArea;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public class OpenAreaService {
    //获取OpenAreaDao实例
    private OpenAreaDao openAreaDao = OpenAreaDao.getInstance();
    //新建OpenAreaService对象
    private static OpenAreaService openAreaService = new OpenAreaService();
    //构造器
    private OpenAreaService(){}
    //获取OpenAreaService对象
    public static OpenAreaService getInstance(){
        return openAreaService;
    }

    /**
     * 添加课题
     * @param openArea
     * @return
     * @throws SQLException
     */
    public boolean add(OpenArea openArea) throws SQLException {
        //获取数据库链接
        Connection connection = JdbcHelper.getConn();
        boolean added = false;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            added = openAreaDao.add(connection,openArea);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return added;
    }

    /**
     * 根据id查找课题
     * @param id
     * @return
     * @throws SQLException
     */
    public OpenArea find(int id) throws SQLException {
        //获取数据库链接
        Connection connection = JdbcHelper.getConn();
        OpenArea openArea = null;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            openArea = openAreaDao.find(connection,id);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return openArea;
    }

    /**
     * 条件分页查找课题
     * @param conditions
     * @param pagination
     * @return
     * @throws SQLException
     */
    public Collection<OpenArea> findAll(Collection<Condition> conditions , Pagination pagination) throws SQLException {
        //获取数据库链接
        Connection connection = JdbcHelper.getConn();
        Collection<OpenArea> openAreas =null;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            openAreas = openAreaDao.findAll(connection,conditions,pagination);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return openAreas;
    }

    /**
     * 删除课题
     * @param id
     * @return
     * @throws SQLException
     */
    public boolean delete(int id) throws SQLException {
        //获取数据库链接
        Connection connection = JdbcHelper.getConn();
        boolean deleted = false;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            deleted = openAreaDao.delete(connection,id);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    /**
     * 更新课题
     * @param openArea
     * @return
     * @throws SQLException
     */
    public boolean update(OpenArea openArea) throws SQLException {
        //获取数据库链接
        Connection connection = JdbcHelper.getConn();
        boolean updated = false;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            updated = openAreaDao.update(connection,openArea);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return updated;
    }
}
